// pages/qiandao/qiandao.js
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentYear:2018,
    currentMonth:11,
    chou:false,
    qian:"签到"
  },
  // 签到
  calendar_button:function(e){
    wx.showLoading({
      title: '',
    })
    var that = this;
  
    
    console.log(e)
    wx.request({
      url: app.globalData.url +'qiandao.php?Action=huiyuanqiandao',
      data: {
        openid: app.globalData.opid
      },
      success: function (res) {
        console.log(res)
        if (res.data.success == 1){
          wx.request({
            url: app.globalData.url + 'qiandao.php?Action=choujiangjiner',
            data:{
              openid : app.globalData.opid
            },
            success:function(res){
              console.log(res)
              that.setData({
                jine: res.data
              })
             wx.request({
               url: app.globalData.url +'qiandao.php?Action=gengxinzhanghu',
               data:{
                 openid:app.globalData.opid ,
                 money:res.data
               },
               success:function(res){
                 console.log(res)
                 wx.hideLoading()
                 that.setData({
                   chou: true,
                   qian: "已签到"
                 })
                 that.qian();
                
                 console.log("formid" + e.detail.formId)
                 console.log("openid" + app.globalData.opid)
                 wx.request({
                   url: app.globalData.url +'comm.php',
                   data:{
                     openid:app.globalData.opid,
                     formid: (e.detail.formId == "the formId is a mock one"?"":e.detail.formId)
                   },
                   success:function(res){
                     console.log(res)
                     wx.hideLoading()
                     
                   }
                 })
               }
             })
            }
          })
        }else{
          that.setData({
            qian: "已签到"
          })
          wx.showToast({
            title: res.data.msg,
            icon:"none"
            
          })
        }

      }
    })



  },
  hide_model: function () {
    var that = this;
    that.setData({
      chou: false
    })
    that.get_time_arr()
  },

  // 获取某年某月总共多少天
  getDateLen(year, month) {
    let actualMonth = month - 1;
    let timeDistance = +new Date(year, month) - +new Date(year, actualMonth);
    return timeDistance / (1000 * 60 * 60 * 24);
  },
  // 获取某月1号是周几
  getFirstDateWeek(year, month) {
    console.log(new Date(year, month - 1, 1).getDay())
    return new Date(year, month - 1, 1).getDay()

  },
  // 获取当月数据，返回数组
  getCurrentArr() {
    let currentMonthDateLen = this.getDateLen(this.data.currentYear, this.data.currentMonth) // 获取当月天数
    let currentMonthDateArr = [] // 定义空数组
    if (currentMonthDateLen > 0) {
      for (let i = 1; i <= currentMonthDateLen; i++) {
       
        
        currentMonthDateArr.push({
          month: 'current', // 只是为了增加标识，区分上下月
          date: i,
          time: this.data.currentYear + "-" + this.data.currentMonth + "-" + i
        })
      }
    }
    this.setData({
      currentMonthDateLen
    })
    return currentMonthDateArr
  },

  // 上月 年、月
  preMonth(year, month) {
    if (month == 1) {
      return {
        year: --year,
        month: 12
      }

    } else {
      return {
        year: year,
        month: --month
      }
    }
  },
  // 切换上个月
  prev_button_month(){
    var year = this.data.currentYear;
    var month = this.data.currentMonth;

    if (month == 1) {
      this.setData({
        currentYear: --year,
        currentMonth: 12
      })

    } else {
      this.setData({
        currentYear: year,
        currentMonth: --month
      })
     
    }
    this.get_time_arr()

    
  },

  // 切换下个月
  next_button_month(){
    var year = this.data.currentYear;
    var month = this.data.currentMonth;

    if (month == 12) {
      this.setData({
        currentYear: ++year,
        currentMonth: 1
      })

    } else {
      this.setData({
        currentYear: year,
        currentMonth: ++month
      })

    }
    this.get_time_arr()
  },

  // 获取当月中，上月多余数据，返回数组
  getPreArr() {
    console.log(123)
    let preMonthDateLen = this.getFirstDateWeek(this.data.currentYear, this.data.currentMonth) // 当月1号是周几 == 上月残余天数）
    let preMonthDateArr = [] // 定义空数组
    if (preMonthDateLen > 0) {
      let { year, month } = this.preMonth(this.data.currentYear, this.data.currentMonth) // 获取上月 年、月
      let date = this.getDateLen(year, month) // 获取上月天数
      for (let i = 0; i < preMonthDateLen; i++) {
        preMonthDateArr.unshift({ // 尾部追加
          month: 'pre', // 只是为了增加标识，区分当、下月
          date: date
        })
        date--
      }
    }
    this.setData({
      preMonthDateLen
    })
    return preMonthDateArr
  },

  // 下月 年、月
  nextMonth(year, month) {
    if (month == 12) {
      return {
        year: ++year,
        month: 1
      }
    } else {
      return {
        year: year,
        month: ++month
      }
    }
  },

  // 获取当月中，下月多余数据，返回数组
  getNextArr() {
    let nextMonthDateLen = 42 - this.data.preMonthDateLen - this.data.currentMonthDateLen // 下月多余天数
    let nextMonthDateArr = [] // 定义空数组
    if (nextMonthDateLen > 0) {
      for (let i = 1; i <= nextMonthDateLen; i++) {
        nextMonthDateArr.push({
          month: 'next',// 只是为了增加标识，区分当、上月
          date: i
        })
      }
    }
    return nextMonthDateArr
  },
  /**
   * 生命周期函数--监听页面加载
   */
  get_time_arr:function(){
    wx.showLoading({
      title: '数据加载中',
    })
    let that = this;
    var dang = this.getCurrentArr();
    var shang = this.getPreArr();
    var zong = shang.concat(dang)
    // console.log(zong)
    var shengyu = 42 - zong.length;
    // console.log(shengyu)
    if (shengyu >= 7) {
      shengyu = shengyu - 7;
      for (var i = 0; i < shengyu; i++) {
        zong.push({ month: "next", date: i + 1 })
      }
    } else {
      for (var i = 0; i < shengyu; i++) {
        zong.push({ month: "next", date: i + 1 })
      }
    }
    // 签到列表
    wx.request({
      url: app.globalData.url + 'qiandao.php?Action=qiaodaolist',
      data: {
        openid: app.globalData.opid
      },
      success: function (res) {
       
        console.log(res.data)
        zong.map((i)=>{
          res.data.map((n)=>{     
            if(i.time == n.d_time){
              i.status = true
            }
          })
        })
        // console.log(zong)
        that.setData({
          zong_arr: zong,
          qiandao_num:res.data.length
        })
        wx.hideLoading()
      },
      fail:function(res){
        wx.hideLoading()
        wx.showToast({
          title: '网络开小差了',
          icon:"none"
        })
      }
    })
   
  },
  qian(){
    var that =this;

    // 签到记录
    wx.request({
      url: app.globalData.url + 'qiandao.php?Action=qiaodaojilv',
      data: {
        openid: app.globalData.opid
      },
      success: function (res) {
        console.log(res)
        that.setData({
          lxqd: res.data.lxqd,
          ljqd: res.data.ljqd,
          zhuangtai:res.data.zhuangtai
        })

      }
    })
  },
  onLoad: function (options) {
   
    var that = this;
    this.qian()
   

   

    // 获取当前年份 月份 日期
    var date = new Date()
    var myDate = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' +
      date.getDate();
      console.log(myDate)

    this.setData({
      currentYear: date.getFullYear(),
      currentMonth: date.getMonth() + 1,
      currentDay: date.getDate(),
      myDate: myDate
    })


    // 获取日期数组
    this.get_time_arr()
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})